# -*- coding: utf-8 -*-

from re import search
import web 
import getData

### Url mappings

urls = ("/", "Index",
        "/search", "Search", 
        '/(js|css|img)/(.*)','static' )


### Templates
render = web.template.render("templates", base="base")
class static:
    def GET(self, media, file):
        try:
            f = open(media+'/'+file, 'rb')
            return f.read()
        except:
            return ''
class Index:
  form = web.form.Form(
        web.form.Textarea(
            "content", web.form.notnull, rows=30, cols=80, description='', placeholder="请输入url列表，用换行分隔"
        ),       
        web.form.Button("查询")
        # ,
        # validators = [web.form.Validator("urls不能为空.", lambda i: i.content == "")]
    )
 
  def GET(self):
    form=self.form
    return render.index(form)
  def POST(self):
    # if form.d.content=="":
    #   raise web.seeother("/tip")
    form = self.form() 
    if not form.validates():
           return render.new(form)    
    # print(form.d.title) 
    web.setcookie('urls', form.d.content)
    raise web.seeother("/search")
 
class Search:
   def GET(self):      
      urls=web.cookies().get('urls')
      # urlArr=urls.split(",")
      urlArr=urls.split("\r\n")
      print(urls)
      # print(urlArr)
      posts=[]
      for url in urlArr:
        # print(getData.getData(url))
        result=getData.getData(url)
        item={'url':url,'title':result[3],'answerCount':result[0],'numberBoard_itemValue1':result[1],'numberBoard_itemValue2':result[2]}
        print(item)
        posts.append(item)
     
      print(posts)
      return render.result(posts)
  
app = web.application(urls, globals())

if __name__ == "__main__":
    app.run()
